package uk.co.q3c.deplan.server.util;

import java.io.IOException;
import java.util.ArrayList;

import org.jdom.JDOMException;

import uk.co.q3c.deplan.client.domain.resource.Resource;
import uk.co.q3c.deplan.client.domain.resource.ResourcePool;
import uk.co.q3c.deplan.client.domain.task.Task;
import uk.co.q3c.deplan.client.domain.task.Task;

/**
 * Import an XML file which is held in dePlan format
 * 
 * @see XMLExport
 * @author DSowerby 8 Oct 2008
 * 
 */
public class XMLImport_dePlan extends DePlanXML implements XMLImporter {

	class TaskRef {
		Task task;
		int ref;
	}

	// private DatabaseConnection targetDb;
	private String sourceFile;
	ResourcePool pool = null;
	ArrayList<TaskRef> successorRefs = new ArrayList<TaskRef>();
	ArrayList<TaskRef> predecessorRefs = new ArrayList<TaskRef>();
	Task rootTask;

	//
	public void start() throws JDOMException, IOException {
		// SAXBuilder builder = new SAXBuilder();
		// Document document = builder.build(sourceFile);
		// Element rootElement = document.getRootElement();
		//
		// DatabaseVersion dbv = new DatabaseVersion();
		// dbv.readXML(this, rootElement.getChild(DBVERSION));
		// logger.info("Server version loaded");
		//
		// targetDb.getAdminDao().save(dbv);
		// targetDb.commit();
		//
		// BaseCalendar baseCalendar = new BaseCalendar();
		// baseCalendar.readXML(this, rootElement.getChild(BASE_CALENDAR));
		// logger.info("Base calendar loaded");
		// targetDb.getBaseCalendarDao().save(baseCalendar);
		// targetDb.commit();
		//
		// // pool = new ResourcePool();
		// // pool.readXML(this, rootElement.getChild(RESOURCE_POOL));
		// // pool.setBaseCalendar(baseCalendar);
		// // for (IndividualResource resource : pool.individuals()) {
		// // targetDb.getResourceDao().save(resource);
		// // }
		// // for (ResourceGroup resource : pool.groups()) {
		// // targetDb.getResourceDao().save(resource);
		// // }
		// // logger.info("Resource pool loaded");
		// // targetDb.getResourceDao().save(pool);
		// // targetDb.commit();
		//
		// Project mp = new Project();
		// mp.readXML(this, rootElement.getChild(TASK));
		// rootTask = mp;
		// logger.info("Tasks loaded");
		// targetDb.getTaskDao().save(mp);
		// targetDb.commit();
		//
		// deReference(mp);
		// // BaseTask t = (BaseTask)
		// // Server.getTaskDao().findById(409);
		// // logger.info(t.predecessorsString());
		// logger.info("References resolved");
		// targetDb.getTaskDao().save(mp);
		// targetDb.commit();
		// // t = (BaseTask) Server.getTaskDao().findById(409);
		// // logger.info(t.predecessorsString());
		//
		// logger.info("Import complete");
		// targetDb.close();
		// targetDb.open();
		// // t = (BaseTask) Server.getTaskDao().findById(409);
		// // logger.info("db in use is ... " + Server.getDb());
		// // logger.info(t.predecessorsString());
	}

	/**
	 * 
	 * @see XMLImporter#deReference()
	 */
	// @Override
	public void deReference(Task masterProject) {
		// logger.info("deReferencing predecessors");
		// for (TaskRef taskRef : predecessorRefs) {
		// Task pred = rootTask.findById(taskRef.ref);
		// if (pred instanceof Task) {
		// logger.info(taskRef.task.qualifiedName() + " has predecessor of " +
		// pred.qualifiedName());
		// taskRef.task.predecessors().add(pred);
		// pred.successors().add(taskRef.task);
		// targetDb.getTaskDao().save(taskRef.task);
		// targetDb.getTaskDao().save(pred);
		//
		// } else {
		// logger.warn("Task " + taskRef.task.qualifiedName() +
		// " has invalid predecessor, of taskId " + taskRef.ref);
		// }
		// }

	}

	// public void setTarget(DatabaseConnection targetDb) {
	// this.targetDb = targetDb;
	// }

	public void setSource(String sourceFile) {
		this.sourceFile = sourceFile;
	}

	/**
	 * 
	 * This method requires that the pool already contains Individual Resources
	 * 
	 * @return
	 */
	@Override
	public Resource resolveResource(String reference) {
		return pool.getResource(reference);
	}

	@Override
	public void resolvePredecessor(Task task, String reference) {
		TaskRef ref = new TaskRef();
		ref.task = task;
		ref.ref = Integer.parseInt(reference);
		predecessorRefs.add(ref);
	}

	/**
	 * 
	 * @see XMLImporter#resolveSuccessor
	 */
	@Override
	public void resolveSuccessor(Task task, String reference) {
		TaskRef ref = new TaskRef();
		ref.task = task;
		ref.ref = Integer.parseInt(reference);
		successorRefs.add(ref);

	}

	@Override
	public void save(Object object) {
		// targetDb.getOodb().store(object);
		throw new RuntimeException("Not yet implemented: " + "XMLIMport_dePlan.save()");
	}
}
